package com.zhsm.fuxi.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import com.baidu.mapapi.map.BitmapDescriptor
import com.baidu.mapapi.map.BitmapDescriptorFactory
import com.zhsm.paipaiout.baselibrary.utils.SPUtils
import com.zhsm.paipaiout.baselibrary.utils.UIUtils.dip2px
import com.zhsm.fuxi.app.Constant
import java.util.regex.Pattern


/**
 * Created by GuZhC on 2018/10/11.
 * describe:
 */
object CommonUtils {
    fun getBitmapDescriptor(context: Context, imgResId: Int, imgW: Float, imgH: Float): BitmapDescriptor? {
        var bitmap = BitmapFactory.decodeResource(context.resources, imgResId)
        val width = bitmap.width
        val height = bitmap.height
        val newWidth = dip2px(context, imgW.toFloat())
        val newHeight = dip2px(context, imgH.toFloat())
        //计算压缩的比率
        val scaleWidth = newWidth.toFloat() / width
        val scaleHeight = newHeight.toFloat() / height
        //获取想要缩放的matrix
        val matrix = Matrix()
        matrix.postScale(scaleWidth, scaleHeight)
        //获取新的bitmap
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true)
        return BitmapDescriptorFactory.fromBitmap(bitmap)
    }

    fun getLatitude(): Double {
        return try {
            SPUtils.getString(Constant.SP_KEY.LAT).toDouble()
        } catch (e: Exception) {
            30.6799428454  //成都中心
        }
    }

    fun getLongitude(): Double {
        return try {
            SPUtils.getString(Constant.SP_KEY.LONG).toDouble()
        } catch (e: Exception) {
            104.067923463  //成都中心
        }
    }

    fun savaLatLon(latitude: Double, longitude: Double) {
        SPUtils.put(Constant.SP_KEY.LAT, latitude.toString())
        SPUtils.put(Constant.SP_KEY.LONG, longitude.toString())
    }

    fun checkPhoneNum(num: String): Boolean{
        val regExp = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(14[5-9])|(166)|(19[8,9])|)\\d{8}$"
        val p = Pattern.compile(regExp)
        val m = p.matcher(num)
        return m.matches()
    }

    fun checkIDVardNum(text: String): Boolean {
        val regx = "[0-9]{17}x"
        val reg1 = "[0-9]{15}"
        val regex = "[0-9]{18}"
        return text.matches(regx.toRegex()) || text.matches(reg1.toRegex()) || text.matches(regex.toRegex())
    }
}